<?php 


require "../common/Curl.php";

/**
 *	https://open.esign.cn/doc/detail?id=opendoc%2Fpaas_api%2Fnyvhmb&namespace=opendoc%2Fpaas_api
 * 	电子签名SaaS API非标版
 * 	机构签署账号API----接口顺序排列封装
 *  使用请求签名鉴权方式
 *  下一个------------------ 静默签署授权silent.php
 */

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
 * [createOrgan 根据创建人accountid创建机构签署账号]    有用    
 * @param  [type] $thirdPartyUserId [创建机构账号的唯一标识。可将企业证件号、企业邮箱地址等作为此账号的唯一标识。]
 * @param  [type] $creator          [创建人个人账号id  创建个人签署账号接口返回的accountid]
 * @param  [type] $name             [组织机构名称]
 * @param  [type] $idType           [机构证件类型--统一社会信用代码]
 * @param  [type] $idNumber         [机构证件号]
 * @param  [type] $orgLegalIdNumber [机构法定代表人证件号]
 * @param  [type] $orgLegalName     [机构法定代表人名称]
 * @param  [type] $header           [请求头] 
 * @param  string $url              [请求地址]                                              aba7702a0fc64f7d99ee158e3767421a
 * @return [json]                   [返回机构签署账号id] {"code":0,"message":"成功","data":{"orgId":"aba7702a0fc64f7d99ee158e3767421a"}
 */
function createOrgan($thirdPartyUserId,$creator,$name,$idType,$idNumber,$orgLegalIdNumber,$orgLegalName,$appId,$secret,$url){
	$urlss = $url."/v1/organizations/createByThirdPartyUserId";
	$head_url = "/v1/organizations/createByThirdPartyUserId";
    $data = [
		"thirdPartyUserId"=>$thirdPartyUserId,
		"creator"=>$creator,
	    "name"=>$name,
	    "idType"=>$idType,
	    "idNumber"=>$idNumber,
	    "orgLegalIdNumber"=>$orgLegalIdNumber,
	    "orgLegalName"=>$orgLegalName
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}                  
$thirdPartyUserId = "15179798888";  //创建机构账号的唯一标识。可将企业证件号、企业邮箱地址等作为此账号的唯一标识。
$creator = "f120cf3f2a124fc88f8438d6c919b8b6";    //创建人个人账号id  创建个人签署账号接口返回的accountid
$name = "赣州刘颂晶网络";   //组织机构名称
$idType = "CRED_ORG_USCC";  //机构证件类型
$idNumber = "546512395648525649";  //机构证件号
$orgLegalIdNumber = "360731199907050379";    //机构法定代表人证件号
$orgLegalName = "liusongjing";  //机构法定代表人名称
$a = createOrgan($thirdPartyUserId,$creator,$name,$idType,$idNumber,$orgLegalIdNumber,$orgLegalName,$appId,$secret,$url);
var_dump($a);   //  "orgId":"aba7702a0fc64f7d99ee158e3767421a"
die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
 * [updateOrgan 根据机构账号id修改机构名称 法人证件号和法人代表名称]   有用   
 * @param  [type] $orgId            [机构签署账号ID]
 * @param  [type] $name             [机构名称]
 * @param  [type] $orgLegalIdNumber [机构法定代表人证件号]
 * @param  [type] $orgLegalName     [机构法定代表人名称]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回修改后机构信息]
 */
function updateOrgan($orgId,$name,$orgLegalIdNumber,$orgLegalName,$appId,$secret,$url){
	$urlss = $url."/v1/organizations/".$orgId;
	$head_url = "/v1/organizations/".$orgId;
	$data = [
		'name'=>$name,
		"orgLegalIdNumber"=>$orgLegalIdNumber,
	    "orgLegalName"=>$orgLegalName
	];
	$res = json_encode($data);
    $header = getHeader($data,"PUT",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="PUT",$urlss,$header,$res);
    return $result;
}
// $orgId = "aba7702a0fc64f7d99ee158e3767421a";
// $name = '赣州刘颂晶网络';
// $a = updateOrgan($orgId,$name,$orgLegalIdNumber,$orgLegalName,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [updateOrganThirdId 根据机构唯一标识修改机构名称 法人证件号和法人代表名称]  有用  
 * @param  [type] $thirdPartyUserId [机构唯一标识]
 * @param  [type] $name             [机构名称]
 * @param  [type] $orgLegalIdNumber [机构法定代表人证件号]
 * @param  [type] $orgLegalName     [机构法定代表人名称]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回修改后机构信息]
 */
function updateOrganThirdId($thirdPartyUserId,$name,$orgLegalIdNumber,$orgLegalName,$appId,$secret,$url){
	$urlss = $url."/v1/organizations/updateByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$head_url = "/v1/organizations/updateByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$data = [
		'name'=>$name,
		"orgLegalIdNumber"=>$orgLegalIdNumber,
	    "orgLegalName"=>$orgLegalName
	];
	$res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}
// $thirdPartyUserId = "15179798888";
// $name = '赣州刘颂晶网络';
// $a = updateOrganThirdId($thirdPartyUserId,$name,$orgLegalIdNumber,$orgLegalName,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectOrgan 根据机构orgId查找机构信息]     有用  
 * @param  [type] $orgId  [机构签署账号ID]
 * @param  [type] $header [请求头]
 * @param  string $url    [请求地址]
 * @return [json]         [返回机构的信息]
 */
function selectOrgan($orgId,$appId,$secret,$url){
	$urlss = $url."/v1/organizations/".$orgId;
	$head_url = "/v1/organizations/".$orgId;
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $orgId = "aba7702a0fc64f7d99ee158e3767421a";
// $a = selectOrgan($orgId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [selectOrganThirdId 根据创建机构账号的唯一标识查找机构信息]   有用   
 * @param  [type] $thirdPartyUserId [创建机构账号的唯一标识]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回机构的信息]
 */
function selectOrganThirdId($thirdPartyUserId,$appId,$secret,$url){
	$urlss = $url."/v1/organizations/getByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$head_url = "/v1/organizations/getByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"GET",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="GET",$urlss,$header,$res);
    return $result;
}
// $thirdPartyUserId = "15179798888";
// $a = selectOrganThirdId($thirdPartyUserId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [deleteOrgan 根据机构orgid账号id注销]    有用    
 * @param  [type] $orgId  [机构账号orgId]
 * @param  [type] $header [请求头]
 * @param  string $url    [请求地址]
 * @return [json]         [返回成功]
 */
function deleteOrgan($orgId,$appId,$secret,$url){
	$urlss = $url."/v1/organizations/".$orgId;
	$head_url = "/v1/organizations/".$orgId;
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"DELETE",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="DELETE",$urlss,$header,$res);
    return $result;
}
// $orgId = "aba7702a0fc64f7d99ee158e3767421a";
// $a = deleteOrgan($orgId,$appId,$secret,$url);
// var_dump($a);die;

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/**
 * [deleteOrganThirdPartyUserId 根据机构账号唯一标识注销机构账号]    有用   
 * @param  [type] $thirdPartyUserId [机构账号唯一标识]
 * @param  [type] $header           [请求头]
 * @param  string $url              [请求地址]
 * @return [json]                   [返回成功]
 */
function deleteOrganThirdPartyUserId($thirdPartyUserId,$appId,$secret,$url){
	$urlss = $url."/v1/organizations/deleteByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$head_url = "/v1/organizations/deleteByThirdId?thirdPartyUserId=".$thirdPartyUserId;
	$data = [];
	$res = json_encode($data);
    $header = getHeader($data,"DELETE",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="DELETE",$urlss,$header,$res);
    return $result;
}
// $thirdPartyUserId = "15179798888";
// $a = deleteOrganThirdPartyUserId($thirdPartyUserId,$appId,$secret,$url);
// var_dump($a);die;
